using System;
using System.Data;
using System.EnterpriseServices;
using System.Runtime.InteropServices;
using PCSComProduction.ShopFloorControl.DS;
using PCSComProduction.WorkOrder.DS;
using PCSComUtils.Common;
using PCSComUtils.PCSExc;

namespace PCSComProduction.ShopFloorControl.BO
{
	/// <summary>
	/// This is interface for WOUserScheduleBO
	/// </summary>
	public interface IWorkOrderUserScheduleBO //: PCSComUtils.Generic.IObjectBO 
	{
		int AddAndReturnID(object pobjObjectVO, DataSet pdsData);
		void DeleteMasterAndDetail(object pObjectVO, DataSet pdstData);
		void UpdateMasterAndDetail(object pObjectMaster, DataSet pdstData);
		/// <summary>
		/// List all WOScheduleDetail by WOMasterID, WODetailID
		/// </summary>
		/// <returns>DataSet</returns>
		DataSet ListData(int pintWOMasterID, int pintWODetailID);

		object GetObjectWOScheduleMasterVO(int pintWOMasterID, int pintWODetailID);
		DataSet GetRoutingInfoBaseWODetail(int pintWOMasterID, int pintWODetailID);
		DataRow GetItemOrderInfo(int pintWOMasterID, int pintWODetailID);
		DataRow GetDataRowWORouting(int pintWORoutingID);
	}


	/// <summary>
	/// Summary description for WorkOrderUserScheduleBO.
	/// </summary>
	[Guid("2231D66D-3BD5-4333-BFB4-75B73D2C616A")]
	//[Transaction(TransactionOption.Required)]
	public class WorkOrderUserScheduleBO :  IWorkOrderUserScheduleBO
	{
		public WorkOrderUserScheduleBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		/// <summary>
		/// Insert a new record into database
		/// </summary>
		//[AutoComplete(true)]
		public void Add(object pObjectDetail)
		{
		}
		//[AutoComplete(true)]
		public void ListMasterData()
		{
		}

		/// <summary>
		/// Return the DataSet (list of record) by inputing the FieldList and Condition
		/// </summary>
		//[AutoComplete(true)]
		public void UpdateDataSet(DataSet dstData)
		{
		}
		
		/// <summary>
		/// Delete record by condition
		/// </summary>
		//[AutoComplete(true)]
		public void Delete(object pObjectVO)
		{
		}

		/// <summary>
		/// Get the object information by ID of VO class
		/// </summary>
		//[AutoComplete(true)]
		public object GetObjectVO(int pintID, string VOclass)
		{
			return null;
		}

		/// <summary>
		/// Update into Database
		/// </summary>
		//[AutoComplete(true)]
		public void Update(object pObjectDetail)
		{
		}
		

		/// <summary>
		/// add and return id to client
		///		Add Master
		///		Update dataset
		/// </summary>
		/// <param name="pobjObjectVO"></param>
		/// <param name="pdsData"></param>
		/// <author>
		/// Do Manh Tuan
		/// 14-06-2005
		/// </author>
		//[AutoComplete(true)]
		public int AddAndReturnID(object pobjObjectVO, DataSet pdsData)
		{
			try
			{
				//Add Master
				dsWOScheduleMaster = new PRO_WOScheduleMasterDS();
				PRO_WOScheduleMasterVO voWOMaster = (PRO_WOScheduleMasterVO) pobjObjectVO;
				
				voWOMaster.WOScheduleMasterID = dsWOScheduleMaster.AddAndReturnID(pobjObjectVO);
			
				//Update Detail
				dsWOScheduleDetail = new PRO_WOScheduleDetailDS();
				foreach (DataRow drow in pdsData.Tables[0].Rows)
				{
					drow[PRO_WOScheduleDetailTable.WOSCHEDULEMASTERID_FLD] = voWOMaster.WOScheduleMasterID;
				}
				dsWOScheduleDetail.UpdateDataSet(pdsData);

				//return MasterID
				return voWOMaster.WOScheduleMasterID;
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}

		}

		/// <summary>
		/// Delete the WOSchedule Master and all details
		/// </summary>
		/// <param name="pObjectVO"></param>
		/// <param name="pdstData"></param>
		/// <author>
		/// Do Manh Tuan
		/// 14-06-2005
		/// </author>
		//[AutoComplete(true)]
		public void DeleteMasterAndDetail(object pObjectVO, DataSet pdstData)
		{
			try
			{
				//Delete Detail
				foreach (DataRow drow in pdstData.Tables[0].Rows)
				{
					if (drow.RowState != DataRowState.Deleted)
					{
						drow.Delete();
					}
				}
				dsWOScheduleDetail = new PRO_WOScheduleDetailDS();
				dsWOScheduleDetail.UpdateDataSet(pdstData);

				//Delete Master
				dsWOScheduleMaster = new PRO_WOScheduleMasterDS();
				dsWOScheduleMaster.Delete(((PRO_WOScheduleMasterVO) pObjectVO).WOScheduleMasterID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}

		}

		/// <summary>
		/// Get the object information by ID
		/// </summary>
		/// <param name="pintWOMasterID"></param>
		/// <param name="pintWODetailID"></param>
		/// <author>
		/// Do Manh Tuan
		/// 14-06-2005
		/// </author>
		//[AutoComplete(true)]
		public object GetObjectWOScheduleMasterVO(int pintWOMasterID, int pintWODetailID)
		{
			try
			{
				dsWOScheduleMaster = new PRO_WOScheduleMasterDS();
				return dsWOScheduleMaster.GetObjectVO(pintWOMasterID, pintWODetailID);	
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}

		}

		/// <summary>
		/// Get information of WOLine as Item, Quantity, Startdate, Duedate
		/// </summary>
		/// <param name="pintWOMasterID"></param>
		/// <param name="pintWODetailID"></param>
		/// <returns></returns>
		//[AutoComplete(true)]
		public DataRow GetItemOrderInfo(int pintWOMasterID, int pintWODetailID)
		{
			try
			{
				dsWOScheduleDetail = new PRO_WOScheduleDetailDS();
				return dsWOScheduleDetail.GetItemOrderInfo(pintWOMasterID, pintWODetailID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}

		}

		/// <summary>
		/// List of operation
		/// </summary>
		/// <param name="pintWOMasterID"></param>
		/// <param name="pintWODetailID"></param>
		/// <returns></returns>
		/// <author>
		/// Do Manh Tuan
		/// 14-06-2005
		/// </author>
		//[AutoComplete(true)]
		public DataSet GetRoutingInfoBaseWODetail(int pintWOMasterID, int pintWODetailID)
		{
			try
			{
				return new PRO_WORoutingDS().GetRoutingInfoBaseWODetail(pintWOMasterID, pintWODetailID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}

		}

		/// <summary>
		/// List all WOScheduleDetail by WOMasterID, WODetailID
		/// </summary>
		/// <param name="pintWOMasterID"></param>
		/// <param name="pintWODetailID"></param>
		/// <returns>DataSet</returns>
		/// <author>
		/// Do Manh Tuan
		/// 14-06-2005
		/// </author>
		//[AutoComplete(true)]
		public DataSet ListData(int pintWOMasterID, int pintWODetailID)
		{
			try
			{
				dsWOScheduleDetail = new PRO_WOScheduleDetailDS();
				return dsWOScheduleDetail.ListData(pintWOMasterID, pintWODetailID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}

		}

		/// <summary>
		/// Update WOSchedule Master and Detail
		/// </summary>
		/// <param name="pObjectMaster"></param>
		/// <param name="pdstData"></param>
		/// <author>
		/// Do Manh Tuan
		/// 14-06-2005
		/// </author>
		//[AutoComplete(true)]
		public void UpdateMasterAndDetail(object pObjectMaster, DataSet pdstData)
		{
			try
			{
				//update Master
				dsWOScheduleMaster = new PRO_WOScheduleMasterDS();
				dsWOScheduleMaster.Update(pObjectMaster);

				//update detail
				int intMasterID = ((PRO_WOScheduleMasterVO)pObjectMaster).WOScheduleMasterID;
				foreach (DataRow drow in pdstData.Tables[0].Rows)
				{
					if (drow.RowState != DataRowState.Deleted)
					{
						drow[PRO_WOScheduleDetailTable.WOSCHEDULEMASTERID_FLD] = intMasterID;
					}
				}
				dsWOScheduleDetail = new PRO_WOScheduleDetailDS();
				dsWOScheduleDetail.UpdateDataSet(pdstData);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}

		}

		/// <summary>
		/// Get informations of WORouting : Type, Function, WorkCenter
		/// </summary>
		/// <param name="pintWORoutingID"></param>
		/// <returns></returns>
		//[AutoComplete(true)]
		public DataRow GetDataRowWORouting(int pintWORoutingID)
		{
			try
			{
				return new PRO_WORoutingDS().GetDataRowWORouting(pintWORoutingID);	
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private DS.PRO_WOScheduleDetailDS dsWOScheduleDetail;
		private DS.PRO_WOScheduleMasterDS dsWOScheduleMaster;
	}
}
